<template>
  <div class="CarList">
    <van-nav-bar
      title="传智播客.黑马程序员Vue.js"
      style="background-color: #26a2ff; margin-bottom: 5px"
    />

    <div class="goods" v-for="item in commoditys" :key="item.id">
      <div @click="anew">
        <van-switch v-model="item.active" size="24" />
      </div>
      <div class="goods_img">
        <img :src="item.thumb_path" alt="" />
      </div>
      <div class="conter">
        <p>{{ item.title }}</p>
        <div>
          <div class="bottom">
            <span class="money">￥{{ item.sell_price }}</span>
            <van-stepper v-model="item.cou" />
            <van-button type="danger" @click="del(item.id)">删除</van-button>
          </div>
        </div>
      </div>
    </div>
    <div class="count">
      <div class="left">
        <h4>总计(不含运费): {{ AllMoney.money }}</h4>
        <p>已经选择商品件{{ AllMoney.count }},总价￥ {{ AllMoney.money }}元</p>
      </div>
      <div class="right">
        <van-button type="info">去结算</van-button>
      </div>
    </div>
  </div>
</template>
<script>
// 引入商品组件

import { postlist } from "@/apis/shopping";
import bus from "@/utils/Eventbus";
export default {
  data() {
    return {
      commoditys: [],
      //通过本地储存获取商品id，数量
      data: JSON.parse(localStorage.getItem("productList")) || [],
    };
  },
  async mounted() {
    let ids = [];
    if (this.data.length > 0) {
      this.data.forEach((item) => {
        ids.push(item.id);
      });
      let res = (await postlist(ids.join(","))).data.message;
      res.forEach((value) => {
        this.data.forEach((item) => {
          if (value.id == item.id) {
            value.cou = item.cou;
          }
          value.active = false;
        });
        this.commoditys = res;
      });
    }
  },
  computed: {
    // 计算总价格与数量
    AllMoney() {
      let money = 0;
      let count = 0;
      this.commoditys.forEach((commodity) => {
        if (commodity.active == true) {
          money += commodity.sell_price * commodity.cou;
          count += commodity.cou;
        }
      });
      return { money, count };
    },
  },
  methods: {
    // 坑
    anew() {
      let arr = [];
      this.commoditys.forEach((v) => {
        arr.push(v);
      });
      this.commoditys = arr;
    },
    // 删除商品
    del(id) {
      this.commoditys = this.commoditys.filter((i) => {
        return id !== i.id;
      });
      this.data = this.data.filter((j) => {
        console.log(j.id, id);
        j.id = parseInt(j.id);
        return id !== j.id;
      });
      bus.$emit("subtract");
    },
  },
  watch: {
    // 商品数量发生改变时，写入本地
    commoditys: {
      handler() {
        this.data.forEach((value) => {
          this.commoditys.forEach((item) => {
            console.log(1111);
            if (value.id == item.id) {
              value.cou = item.cou;
            }
          });
        });

        window.localStorage.setItem("productList", JSON.stringify(this.data));
      },
      immediate: true,
      deep: true,
    },
  },
};
</script>

<style lang="less" scoped>
.CarList {
  .count {
    display: flex;
    margin-top: 20px;
    align-items: center;
    background: #ccc;
    justify-content: space-between;
    position: fixed;
    bottom: 50px;
    width: 100%;
  }
  .goods {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px;
    height: 80px;
    border-bottom: 1px solid #ccc;
    .goods_img {
      height: 100%;
      img {
        height: 100%;
        padding: 0 10px;
      }
    }
    .bottom {
      margin-top: 10px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .money {
        color: red;
        font-weight: 700;
      }
    }
    .conter > p {
      font-size: 14px;
      font-weight: 600;
    }
  }
}
</style>